package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.enums.eHuaShiGangActivityType;
import com.yanqu.road.entity.enums.ePatronsSkillType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.logic.battle.BattleManager;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/10/7
 */
@Cmd(code = GameProtocol.S_HUA_SHI_GANG_ATTACK_MONSTER, desc = "花石纲攻击怪物")
public class AttackMonsterCmd extends AbstractCommand {

    int patronsId;
    int costNum;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.AttackMonsterReqMsg msg = HuaShiGangProto.AttackMonsterReqMsg.parseFrom(packet.getBytes());
        patronsId = msg.getPatronsId();
        costNum = msg.getCostNum();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = HuaShiGangMgr.getActivityInfoRet(player);
        if (ret != 0) {
            return;
        }
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        if (!ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        if (!HuaShiGangMgr.inOperateTime(activityInfo)) {
            ret = GameErrorCode.E_HUASHIGANG_NOT_OPERATE_TIME;
            return;
        }
        Map<Integer, UserPatrons> userPatronsMap = player.getModule(PatronsModule.class).getUserPatronsMap();
        UserPatrons userPatrons = userPatronsMap.get(patronsId);
        if (userPatrons == null) {
            ret = GameErrorCode.E_PATRONS_NO_FOUND;
            return;
        }

        String costToken = HuaShiGangMgr.getHuaShiGangTokenId() + "=" + costNum;
        if (costNum > 0) {
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(costToken))) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
        }

        player.getModule(PreReduceModule.class).preReduce(costToken);

        int addition = 0;
        List<UserPatrons> list = new ArrayList<>();
        for (UserPatrons pat : player.getModule(PatronsModule.class).getUserPatronsList()) {
            list.add(pat.cloneUserPatrons());
        }

        long curioAddition = CurioMgr.getSkillAdditionBySkillType(eSkillType.HuaShiGangPatronsSkill.getValue(), player, 0);

        // 元夜闹东京 不要这个加成
        if (activityInfo.getChildType() != eHuaShiGangActivityType.TokyoActivity.getValue()) {
            BattleManager.skillEffect(list);
            for (UserPatrons pat : list) {
                if (userPatrons.getPatronsId() == pat.getPatronsId()) {
                    addition = pat.getBattleAddition();
                    break;
                }
            }
        }

        int talentAddition = 0;
        for (UserPatrons patrons : userPatronsMap.values()) {
            for (UserPatronsSkill patronsSkill : patrons.getSkillList()) {
                if (patronsSkill.getType() != ePatronsSkillType.Talent.getValue()) {
                    continue;
                }
                SkillInfo skillInfo = SkillMgr.getSkillInfo(patronsSkill.getSkillId());
                // 蔡京和宋微宗的权势压制对所有花石纲类活动都生效
                if (skillInfo.getType() == eSkillType.HuaShiGangDamageAddition.getValue()) {
                    talentAddition += SkillMgr.getSkillAddition(skillInfo, patronsSkill.getSkillLv());
                }
            }
        }

        talentAddition += curioAddition;

        int bossTalentAddition = 0;
        for (UserPatrons patrons : userPatronsMap.values()) {
            for (UserPatronsSkill patronsSkill : patrons.getSkillList()) {
                if (patronsSkill.getType() != ePatronsSkillType.Talent.getValue()) {
                    continue;
                }
                SkillInfo skillInfo = SkillMgr.getSkillInfo(patronsSkill.getSkillId());
                // 替天行道对所有花石纲类活动都生效
                if (skillInfo.getType() == eSkillType.HuaShiGangBossDamageAddition.getValue()) {
                    bossTalentAddition += SkillMgr.getSkillAddition(skillInfo, patronsSkill.getSkillLv());
                }
            }
        }

        bossTalentAddition += curioAddition;

        HuaShiGangProto.CrossAttackMonsterReqMsg.Builder builder = HuaShiGangProto.CrossAttackMonsterReqMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        builder.setPatronsId(patronsId);
        builder.setCostNum(costNum);
        builder.setTalentAddition(talentAddition);
        builder.setAbility(userPatrons.getAbility().longValue());
        builder.setOccupationAddition(addition);
        builder.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        builder.setBossTalentAddition(bossTalentAddition);
        player.sendPacket(CrossProtocol.C2_HUA_SHI_GANG_ATTACK_MONSTER, builder);


    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.AttackMonsterRespMsg.Builder builder = HuaShiGangProto.AttackMonsterRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_ATTACK_MONSTER, builder);
        }
    }
}
